<!-- 内容主体区域 -->
<link rel="stylesheet" href="layui/css/layui.css">
<div style="padding: 15px;">
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li class="layui-this">技术统计</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-input-inline" style="">
                <div class="layui-input-inline">
                    <button id="add" class="layui-btn layui-btn-normal">新建技术统计</button>
                </div>
            </div>
            <table class="layui-hide" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
            </script>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="layui/layui.js"></script>
<script>
        //     url传参获取
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null)
            return unescape(r[2]);
        return null; //返回参数值
    }

    
    layui.use(['table', 'form'], function() {
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#test',
            url: 'http://svcsa.org/ctfcstatistics?matchid='+getUrlParam('MatchID'),
            parseData: function(res) {
                return {
                    "code": 0, //解析接口状态
                    "msg": '', //解析提示文本
                    "count": res.totalcount, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            method: 'get',
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                ,limitName: 'pagesize' //每页数据量的参数名，默认：limit
            },
            title: '用户数据表',
            cols: [
                [{
                    field: 'ID',
                    title: 'ID',
                    width: 80,
                    fixed: 'left',
                    unresize: true,
                    sort: true,
                }, {
                    field: 'PlayerID',
                    title: '选手/团队名称',
                    width: 150,
                }, {
                    field: 'WindSpeed',
                    title: '风速',
                    sort: true,
                    width: 150,
                }, {
                    field: 'Result',
                    title: '成绩',
                    width: 100,
                }, {
                    field: 'MatchID',
                    title: '比赛',
                    width: 160,
                },{
                    title: '操作',
                    width: 150,
                    toolbar: '#barDemo'
                }]
            ],
            limit: 20,
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                , groups: 1 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页

            },
        });
        $("#add").click(function() {
            var vv=layer.open({
                type: 2,
                title: '添加成绩',
                shadeClose: true,
                shade: 0.8,
                area: ['50%', '50%'],
                content: 'ctfc_statistics_add.html?matchid='+getUrlParam('MatchID'),
                success: function(layero, index) {
                    layer.iframeAuto(index);
                },
                end: function() {
                    location.reload();
                }
            });
            layer.full(vv)
        })
        table.on('tool(test)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'edit') { //查看
                console.log(obj.data);
                var title="修改成绩"
  
                layer.open({
                    type: 2,
                    title: title,
                    shadeClose: true,
                    shade: 0.8,
                    area: ['50%', '80%'],
                    content: 'ctfc_statistics_edit.html?matchid='+getUrlParam('MatchID'),
                    success: function(layero, index) {
                        layer.iframeAuto(index);
                        var body = layer.getChildFrame('body', index);
                        body.find('#ID').val(data.ID)
                        body.find('#SeasonID').val(data.PlayerID)
                        body.find('#WindSpeed').val(data.WindSpeed)
                        body.find('#Result').val(data.Result)
                        body.find('#MatchID').val(data.MatchID)
                            
                    },
                    end: function() {
                        location.reload();
                    }
                });
            } else if (layEvent === 'del') {
                layer.confirm('确认删除', {
                    btn: ['确认', '取消'] //按钮
                }, function() {
                    $.ajax({
                        url:'http://svcsa.org/CTFCStatistics/' + data.ID,
                        //url:"http://svcsa.org/bbcompetition",
                        type : 'delete',
                        dataType : 'json',
                        header:{
                            Accept:'application/json'
                        },
                        success:function(data){
                            layer.msg('删除成功', {
                                icon: 1,
                                time: 1200
                            }, function() {
                                location.reload();
                            });
                        },
                        error:function(data){
                            console.log(data)
                        }
                    });

                }, function() {});
            }
        });
    });
</script>